package com.zz.utils;

import com.zz.dto.PatientPhysiologistResult;
import com.zz.pojo.Patient;
import com.zz.pojo.Physiology;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * Copyright Zz
 * FileName: PhysiologistUtil
 * Author:   Zz
 * Date:     2023/6/1211:29
 * Description:生理信息工具类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
public class PhysiologistUtil {
    /**
     * 返回前端需要的生理信息数据格式
     * @param patient 查询到的病人信息及其生理信息
     * @return 处理过后的生理信息数据格式
     */
    public static PatientPhysiologistResult getPhysiologistResult(Patient patient){
       LocalDate[] category=new LocalDate[7];
        for (int i = 0; i < 7; i++) {
            category[i]=LocalDate.now().minusDays(6-i);
        }
        Queue<Physiology> queue=new PriorityQueue<>(new Comparator<Physiology>() {
            @Override
            public int compare(Physiology o1, Physiology o2) {// 根据日期升序排序
                return -(int) o1.getDate().until(o2.getDate(), ChronoUnit.DAYS);
            }
        });
        List<Physiology> list=patient.getPhysiologist();
        for (int i=0;i<list.size();i++){
            Physiology physiology=list.get(i);
            int diff= (int) physiology.getDate().until(LocalDate.now(),ChronoUnit.DAYS);
            if (diff>=0&&diff<7){
                queue.add(physiology);
            }
        }
        while (queue.size()<7){
            queue.add(new Physiology(patient.getPatientId(),
                    LocalDate.now().minusDays(6-queue.size()),0.00f,0,0,0.00f));
        }
        list=new ArrayList<>();
        while (!queue.isEmpty()){
            list.add(queue.poll());
        }
        patient.setPhysiologist(list);
       return new PatientPhysiologistResult(patient,category);
    }
}
